﻿using System.Collections.Generic;

namespace ETW.EventListeners.ObjectValues
{
    /// <summary>
    /// 合计类型计数器有效负载
    /// </summary>
    public class SumCounterPayload
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 显示名称
        /// </summary>
        public string DisplayName { get; set; }
        /// <summary>
        /// 显示速率
        /// </summary>
        public string DisplayRateTimeScale { get; set; }
        /// <summary>
        /// 增量
        /// </summary>
        public double Increment { get; set; }
        /// <summary>
        /// 间隔秒
        /// </summary>
        public float IntervalSec { get; set; }
        /// <summary>
        /// 计数器类型
        /// <para>
        /// Sum
        /// </para>
        /// </summary>
        public string CounterType { get; set; }
        /// <summary>
        /// 元数据
        /// </summary>
        public string Metadata { get; set; }
        /// <summary>
        /// 显示单位
        /// </summary>
        public string DisplayUnits { get; set; }

        /// <summary>
        /// 转换成合计类型计数器有效负载
        /// </summary>
        /// <param name="eventPayload"></param>
        /// <returns></returns>
        public static SumCounterPayload ConvertFrom(IDictionary<string, object> eventPayload)
        {
            if (!eventPayload["CounterType"].Equals("Sum"))
                return null;

            return new SumCounterPayload
            {
                Name = eventPayload["Name"].ToString(),
                DisplayName = eventPayload["DisplayName"].ToString(),
                DisplayRateTimeScale = eventPayload["DisplayRateTimeScale"].ToString(),
                Increment = double.TryParse(eventPayload["Increment"].ToString(), out var db) ? db : 0,
                IntervalSec = float.TryParse(eventPayload["IntervalSec"].ToString(), out var fl) ? fl : 0,
                CounterType = "Sum",
                Metadata = eventPayload["Metadata"].ToString(),
                DisplayUnits = eventPayload["DisplayUnits"].ToString(),
            };
        }
    }
}
